package com.zjsru.studyplan1;

import java.util.Arrays;

/**
 * 移动零
 *
 * 输入: [0,1,0,3,12]
 * 输出: [1,3,12,0,0]
 *
 * */

class moveZeroes {
    public void moveZeroes(int[] nums) {
        int n = nums.length;
        int count = 0;
        for (int i = 0; i < n; i++) {
            if (nums[i] != 0) {
                if(i>count){
                    nums[count] = nums[i];
                    nums[i] = 0;
                }
                count++;
            }
        }
    }
    
    /**
     * 双指针，然后移动位数
     *
     * @param nums 全国矿工工会
     */
    public void moveZeroes2(int[] nums) {
        int[] copy = nums.clone();
        Arrays.sort(nums);
        int j = nums.length -1;
        for(int i = 0 ; i < j;i++){
            if(nums[i] == 0){
                int temp = 0;
                temp = nums[i];
                nums[i] = nums[j];
                nums[j] = temp;
                j--;
            }
        }
        if(j == nums.length - 1){
            nums = copy;
            return ;
        }
        for (int i = 0; i <= j; i++) {
            for (int next = i + 1; next <= j; next++) {
                if (nums[i] > nums[next]) {
                    int temp = 0;
                    temp = nums[i];
                    nums[i] = nums[next];
                    nums[next] = temp;
                }
            }
        }
    }
    
    /**
     * 模拟补0
     *
     * @param nums 全国矿工工会
     */
    public void moveZeroes3(int[] nums) {
        int len = nums.length - 1;
        int j = 0;
        //获取不为零的数
        for (int i = 0; i <= len; i++) {
            if (nums[i] != 0) {
                nums[j] = nums[i];
                j++;
            }
        }
        //剩余的位数直接填补0
        for (int i = j; i <= len; i++) {
            nums[i] = 0;
        }
    }

    public static void main(String[] args) {
        moveZeroes moveZeroes = new moveZeroes();
        int[] nums = new int[]{2,1};
        moveZeroes.moveZeroes2(nums);
    }
}
